<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="服务器ip" prop="deviceIp">
        <el-input v-model="queryParams.deviceIp" placeholder="请输入服务器ip" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="调试设备名称" prop="deviceName">
        <el-input v-model="queryParams.deviceName" placeholder="请输入调试设备名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['mes:debugConfig:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['mes:debugConfig:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="prorouteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="服务器ip" align="center" prop="deviceIp">
        <!-- <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['mes:pro:proroute:query']"
          >{{scope.row.deviceIp}}</el-button>
        </template>-->
      </el-table-column>
      <el-table-column label="调试设备名称" align="center" prop="deviceName" />
      <el-table-column label="文件路径" align="center" prop="filePath" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['mes:debugConfig:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['mes:debugConfig:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改工艺路线对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" style="width: 500px;">
        <el-form-item label="服务器ip" prop="deviceIp">
          <el-input v-model="form.deviceIp" placeholder="请输入服务器ip" />
        </el-form-item>

        <el-form-item label="调试设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入调试设备名称" />
        </el-form-item>
        <el-form-item label="文件存放路径" prop="filePath">
          <el-input v-model="form.filePath" placeholder="请输入文件存放路径" />
        </el-form-item>
        <!-- <el-form-item label="解析方式" prop="routeName">
          <el-input v-model="form.routeName" placeholder="请输入解析方式" />
        </el-form-item>

        <el-form-item label="解析类型" prop="enableFlag">
          <el-radio-group v-model="form.enableFlag">
            <el-radio :label="1">国标</el-radio>
            <el-radio :label="2">欧标</el-radio>
          </el-radio-group>
        </el-form-item>-->
        <!-- <el-row>
          <el-col :span="24">
            <el-form-item label="说明" prop="routeDesc">
              <el-input v-model="form.routeDesc" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>-->
      </el-form>
      <div v-if="optType =='edit'">
        <el-tag size="large" style="font-size: 20px; margin-bottom: 10px; margin-top: 10px">调试机参数明细</el-tag>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addAnalysis">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExportAnalysis"
              v-hasPermi="['mes:pro:proroute:export']"
            >导出</el-button>
          </el-col> -->
        </el-row>
        <el-table v-loading="loadinga" :data="itemList">
          <el-table-column label="序号" align="center" width="50px">
            <template slot-scope="scope">
              <span>{{scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="指标名称" align="center" prop="tagName" />
          <el-table-column label="指标取值列" align="center" prop="tagColumn" />
          <el-table-column label="指标取值行" align="center" prop="tagRow" />
          <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="editAnalysis( scope.row)">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="delDetail(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType =='view'">返回</el-button>
        <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <DetailList v-if="openDetail" ref="detailsQuery" @parentList="getAnalysis"></DetailList>
  </div>
</template>

<script>
import { listProroute, getProroute } from "@/api/mes/pro/proroute";
import DetailList from "./detailList.vue";
import {
  datacolList,
  delDatacol,
  addDatacol,
  updateDatacol,
  analysisList,
  delAnalysis
} from "@/api/mes/md/debugConfig";
import moment from "moment";
export default {
  name: "DebugConfig",
  dicts: ["sys_yes_no"],
  components: { DetailList },
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      itemList: [],
      loadinga: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺路线表格数据
      prorouteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        routeCode: null,
        routeName: null,
        routeDesc: null,
        enableFlag: null,
        deviceName: null,
        deviceIp: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceIp: [
          { required: true, message: "服务器ip不能为空", trigger: "blur" }
        ],
        deviceName: [
          { required: true, message: "调试设备名称不能为空", trigger: "blur" }
        ],
        filePath: [
          { required: true, message: "文件存放路径不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工艺路线列表 */
    getList() {
      this.loading = true;
      datacolList(this.queryParams).then(response => {
        this.prorouteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        routeId: null,
        routeCode: null,
        routeName: null,
        routeDesc: null,
        enableFlag: "Y",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.routeId);
      this.queryParams.proRoutIdList = selection.map(item => item.routeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工装调试机配置 ";
      this.optType = "add";
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const routeId = row.routeId || this.ids;
      getProroute(routeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看工装调试机配置";
        this.optType = "view";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row, '修改的row');
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改工装调试机配置 ";
      this.optType = "edit";
      this.getAnalysis();
    },

    // 行表列表、
    getAnalysis() {
      this.loadinga = true;
      let params = {
        equipmentId: this.form.id
      };
      analysisList(params).then(response => {
        this.itemList = response.rows;
        this.loadinga = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDatacol(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDatacol(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal
        .confirm(
          '是否确认删除工装调试机配置名称为"' + row.deviceName + '"的数据项？'
        )
        .then(function () {
          return delDatacol(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    delDetail(row) {
      const id = row.id;
      this.$modal
        .confirm(
          '是否确认删除指标名称为"' + row.tagName + '"的数据项？'
        )
        .then(function () {
          return delAnalysis(id);
        })
        .then(() => {
          this.getAnalysis();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/datacol/info/export",
        {
          ...this.queryParams
        },
        `工装调试机配置_${moment(new Date()).format('yyyyMMDDHHmmss')}.xlsx`
      );
    },
    handleExportAnalysis() {
      this.download(
        "mes/datacol/analysis/export",
        {
          ...this.queryParams
        },
        `datacol_analysis_${new Date().getTime()}.xlsx`
      );
    },

    addAnalysis() {
      this.openDetail = true;
      this.$nextTick(() => {
        this.$refs.detailsQuery.init(this.form, {});
        //清空表单
        this.$refs.detailsQuery.reset()
      });
    },

    editAnalysis(row) {
      this.openDetail = true;
      this.$nextTick(() => {
        this.$refs.detailsQuery.init(this.form, row);
      });
    }
  }
};
</script>
